package com.example.service;

import com.example.entity.Album;
import com.example.entity.Sing;
import com.example.entity.Singer;
import com.example.mapper.AlbumMapper;
import com.example.mapper.SingMapper;
import com.example.mapper.SingerMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * 专辑信息表业务处理
 **/
@Service
public class AlbumService {

    @Resource
    private AlbumMapper albumMapper;
    @Resource
    private SingMapper singMapper;
    @Resource
    private SingerMapper singerMapper;

    /**
     * 新增
     */
    public void add(Album album) {
        albumMapper.insert(album);
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        albumMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            albumMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Album album) {
        albumMapper.updateById(album);
    }

    /**
     * 根据ID查询
     */
    public Album selectById(Integer id) {
        return albumMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Album> selectAll(Album album) {
        return albumMapper.selectAll(album);
    }

    /**
     * 分页查询
     */
    public PageInfo<Album> selectPage(Album album, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Album> list = albumMapper.selectAll(album);
        return PageInfo.of(list);
    }


    public List<Album> selectTop(Integer num) {
        List<Album> albums = albumMapper.selectTop(num);
        for (Album album : albums) {
            List<Sing> list = singMapper.selectTop3ByAlbumId(album.getId());
            for (Sing song : list) {
                Singer singer = singerMapper.selectById(song.getSingerId());
                song.setSingerName(singer.getName());
            }
            album.setSongs(list);
        }
        return albums;
    }
}